from abc import ABC
from abc import abstractmethod
from collections.abc import Sequence
from datetime import datetime

from pydantic import BaseModel
from pydantic import field_validator

from onyx.utils.url import normalize_url


class WebSearchResult(BaseModel):
    title: str
    link: str
    snippet: str | None = None
    author: str | None = None
    published_date: datetime | None = None

    @field_validator("link")
    @classmethod
    def normalize_link(cls, v: str) -> str:
        return normalize_url(v)


class WebContent(BaseModel):
    title: str
    link: str
    full_content: str
    published_date: datetime | None = None
    scrape_successful: bool = True

    @field_validator("link")
    @classmethod
    def normalize_link(cls, v: str) -> str:
        return normalize_url(v)


class WebContentProvider(ABC):
    @abstractmethod
    def contents(self, urls: Sequence[str]) -> list[WebContent]:
        pass


class WebSearchProvider(WebContentProvider):
    @abstractmethod
    def search(self, query: str) -> Sequence[WebSearchResult]:
        pass
